Hello, localized world! in ASP.NET

According to Wikipedia, "Localization is the process of creating an environment conducive to a specific region or language and translating text, adaptation to a specific region or language", and likewise we will see in the next few chapters. By default, offering your website in another language can be a great job, but fortunately, ASP.net has made the process a lot easier.

Here are some concepts that you need to know about the concepts of local and global resources from the Culture Info cell as well as the underlying and obvious localization. In the next chapters, we will see them all, but first of all, we see it in our own "Hello, Localized World" action! Examples.

With the knowledge gained from the previous chapters, let's try to localize some lesson, to see if it works. You should create a new project in Visual Studio, you can use the added default page, or create a new one for the purpose. The content should look like this:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Demo</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label runat="server" ID="lblHelloWorld" Text="Hello, world!" />
    </form>
</body>
</html>

If you run it, you will get good old "Hello, world!" Message, as plainly as expected in English Let's add some localization magic to it. We will use local, built-in localization for this - both are the new terms, which will be explained later. For now, just click on your project and ASP.NET Click the Add Folder menu, select App_LocalResources, this is the folder where we hold local resources, each of them is specific to the file in your project

Now, right click on this new folder, and add a new item it should be a resource file, and the name should be similar to your page, but with the .resx extension. For example, I called my page test.aspx, so my resource file should be named Test.aspx.resx. This part is important - if we want ASP.NET to automatically map a resource file to a specific ASP.NET page, the name should be so. This is our default resource file, which is used to keep the default version of our wires. Let's add two other languages. Once again, the file name is used to map the resource file, for example, to add a German language file, the name should be <filename> .aspx.de.resx, Or in a culture-specific version: <filename> .aspx .de-DE.resx

I tested Aspx.de-DE.resx and Test.aspx.es-ES.resx are added, to translate the page into German and Spanish. Then I'll add a new line to test.aspx.resx, with the name lblHelloWorld.Text In my project, English is the default language, so I call this line "Hello, world!" Let me assume that I open Test.aspx.de-DE.resx then add a line with the same name as before, and "Hallo, Welt!" Set the value for i test Do the same for Aspx.es-ES.resx, where I "Hole, Mundo!" Now all three of your resource files should have a line with the name "lblHelloWorld.Text", and the local version of the Hello World string.

Now, our ASP.NET Go back to the page and use Meta: resource key property on our label control, to use it our resource string. It should look like this:


<asp:Label runat="server" ID="lblHelloWorld" Text="Hello, world!" meta:resourcekey="lblHelloWorld" />

As you can see, I have used the same string for the control id. You probably want to add a resource line with the name "lblHelloWorld.Text", it corresponds to a control with the resource key of "lblHelloWorld", which maps to the text of this control property. Now, try setting up the UICulture property on your page and run the example:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" UICulture="de-DE" %>

The label is now in German, change UICulture into "es-ES" and reload the page. It is now in Spanish then try to completely change it in something differently, such as foreign-fr, and you will see our default language instead, just because we do not have a local version of string in French.

This was a simple example, to show you how it might work, but you need some more information about how it works. In the next chapters, we will also see local and global localization as well as built-in and clear localization. At the top is the Culture Info class, because it is heavily used while doing localization.